package handler

import (
	"context"
	"gozeroStandalone/internal/logic"
	"gozeroStandalone/internal/types"
	"gozeroStandalone/utils"
	"math"
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
)

/*
 +----------------------------------------------------------------------
 + Title        : BannerHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-10-18
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 横幅处理器
 +----------------------------------------------------------------------
*/
type BannerHandler struct {
	BaseHandler
}

/**
 * 新增
 *
 * @param string title ---------------------必传
 * @param string image_url -----------------非必传
 * @param int type -------------------------必传
 * @param string link ----------------------非必传
 * @param int sort -------------------------非必传
 * @param string note ----------------------非必传
 * @return json
 * @author huwl
 */
func (control *BannerHandler) Add(w http.ResponseWriter, r *http.Request) {
	var req types.BannerAddRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	if req.Title == "" {
		control.BaseError(r, w, control.WithMessage("请输入标题"))
		return
	}
	if req.Type == 0 {
		control.BaseError(r, w, control.WithMessage("类型错误"))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Add(req)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("新增横幅成功"))
}

/**
 * 删除
 *
 * @param string ids -----------------------必传
 * @return json
 * @author huwl
 */
func (control *BannerHandler) Delete(w http.ResponseWriter, r *http.Request) {
	var req types.BannerDeleteRequest
	if err := httpx.Parse(r, &req); err != nil {
		httpx.Error(w, err)
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Delete(req)

	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("删除横幅成功"))
}

/**
 * 编辑
 *
 * @param int id ---------------------------必传
 * @param string title ---------------------非必传
 * @param string image_url -----------------非必传
 * @param int type -------------------------非必传
 * @param string link ----------------------非必传
 * @param int sort -------------------------非必传
 * @param string note ----------------------非必传
 * @return json
 * @author huwl
 */
func (control *BannerHandler) Edit(w http.ResponseWriter, r *http.Request) {
	var req types.BannerEditRequest
	if err := httpx.Parse(r, &req); err != nil {
		httpx.Error(w, err)
		return
	}

	if req.Id <= 0 {
		control.BaseError(r, w, control.WithMessage("Id错误"))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.Edit(req)

	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("编辑横幅成功"))
}

/**
 * 编辑状态
 *
 * @param int id ---------------------------必传
 * @param int status -----------------------非必传
 * @return json
 * @author huwl
 */
func (control *BannerHandler) EditStatus(w http.ResponseWriter, r *http.Request) {
	var req types.BannerEditStatusRequest
	if err := httpx.Parse(r, &req); err != nil {
		httpx.Error(w, err)
		return
	}

	if req.Id <= 0 {
		control.BaseError(r, w, control.WithMessage("Id错误"))
		return
	}
	if !utils.IsContainNumber[int8]([]int8{-1, 1}, req.Status) {
		control.BaseError(r, w, control.WithMessage("状态错误"))
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	err := bannerLogic.EditStatus(req)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	message := ""
	if req.Status == 1 {
		message = "启用横幅"
	} else {
		message = "禁用横幅"
	}

	control.BaseSuccess(r, w, control.WithMessage(message+"成功"))
}

/**
 * 列表
 *
 * @param int id ---------------------------非必传,Id
 * @param string ids -----------------------非必传,Id,支持多个
 * @param string title ---------------------非必传,标题
 * @param int type -------------------------非必传,类型
 * @param string Status --------------------非必传,状态支持多个：1,-1
 * @param string StartCreateTime -----------非必传,开始创建时间
 * @param string EndCreateTime -------------非必传,结束创建时间
 * @param string SortField -----------------非必传,排序字段
 * @param string SortType ------------------非必传,排序方式
 * @param int PageSize ---------------------非必传,条数
 * @param int Page -------------------------非必传,页码
 * @return json
 * @author huwl
 */
func (control *BannerHandler) List(w http.ResponseWriter, r *http.Request) {
	var req types.BannerQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		httpx.Error(w, err)
		return
	}

	page := req.Page
	pageSize := req.PageSize
	if req.Page <= 0 {
		page = 1
	}
	if req.PageSize <= 0 {
		pageSize = 10
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	banners, total, err := bannerLogic.List(req, page, int8(pageSize))
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"page":       page,
		"page_size":  pageSize,
		"total":      total,
		"page_total": math.Ceil(float64(total) / float64(pageSize)),
		"banners":    banners,
	}))
}

/**
 * 数量
 *
 * @param int id ---------------------------非必传,Id
 * @param string ids -----------------------非必传,Id,支持多个
 * @param string title ---------------------非必传,标题
 * @param int type -------------------------非必传,类型
 * @param string Status --------------------非必传,状态支持多个：1,-1
 * @param string StartCreateTime -----------非必传,开始创建时间
 * @param string EndCreateTime -------------非必传,结束创建时间
 * @return json
 * @author huwl
 */
func (control *BannerHandler) Total(w http.ResponseWriter, r *http.Request) {
	var req types.BannerQueryRequest
	if err := httpx.Parse(r, &req); err != nil {
		httpx.Error(w, err)
		return
	}

	// 调用逻辑层
	bannerLogic := logic.NewBannerLogicInstance(context.Background(), control.SvcCtx)
	total, err := bannerLogic.Total(req)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"total": total,
	}))
}
